package com.hz.framework.ajax;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.hz.framework.check.Application;
import com.hz.framework.check.DesEncrypt;
import com.hz.framework.dao.CommonDBDao;
import com.hz.framework.dao.CommonDao;
import com.hz.framework.util.DateUtil;
import com.hz.framework.util.SqlParameter;
import com.hz.supermanage.bean.HzsysPrograma;
import com.hz.supermanage.bean.Hzsyspersonnel;
import com.hz.ys_stock.dao.StockDao;
import com.hz.ys_stock.dbsql.StockSql;
import com.hz.ys_system.dao.SystemDao;



/*  公共DWR方法类  */
public class BPComDwr extends CommonDao {
	
	private int workingday = 0;
	
	private String DateEnd = "";
	private List lstValue=null;
	private boolean flag = false;
	
//	从数据库取出所有需要的树
	public List getLeft() {
		
		String hql = "";
		
		List<HzsysPrograma> l = new ArrayList() ;
		//所有集合
		List allList = new ArrayList();
		//子类树集合
		List sl = new ArrayList() ;
		//父类树集合
		List fl = new ArrayList() ;
		List<HzsysPrograma> tl = new ArrayList() ;
		//父类树数据转换
		hql = "from HzsysPrograma where parentid='-1' and prostate='A' order by proorder";
		
		List<HzsysPrograma> tempList = (List)selHqlList(hql) ;
		for(HzsysPrograma lmm : tempList) {
			if(lmm != null){
				Map mf = new HashMap<String, Comparable>() ;
				mf.put("id", lmm.getProid()) ;
				mf.put("parentId", lmm.getParentid()) ;
				mf.put("nodeName", lmm.getProname()) ;
				mf.put("link", lmm.getProcontent()) ;
				l.add(lmm);
				fl.add(mf) ;
			}
		}
		
		
		allList.add(fl);
		//根据上面的查询返回的值,判断出子类树究竟有多少,并进行查询,这样做的好处是,易于扩展,松耦合
		for(int v = 0 ; v < l.size() ; v ++)
		{
			int id = l.get(v).getProid() ;
			//查询具体子树
			hql = "from HzsysPrograma where parentid = '" + id +"' and prostate='A' order by  proorder";
			tl.clear();
			tl = selHqlList(hql) ;
			List temp = new ArrayList() ;
			//子类树进行转换
			for(HzsysPrograma lm : tl)
			{
				Map m = new HashMap<String, Comparable>() ;
				m.put("id", lm.getProid()) ;
				m.put("parentId", lm.getParentid()) ;
				m.put("nodeName", lm.getProname()) ;
				m.put("link", lm.getProcontent()) ;
				temp.add(m) ;
			}
			allList.add(temp) ;
			
		}
		return allList;
	
	}
	
	/**
	 * @author 韩飞
	 * @time Jul 14, 2011	
	 * @param keyValue
	 * @return 
	 * 方法功能: 通过固定密钥 加密信息
	 */
	public static String doEncString(String keyValue) {
		if(!"".equals(keyValue)){
			DesEncrypt des = DesEncrypt.getInstance();
			des.setKey(Application.KEY );
			String envalue = des.getEncString(keyValue);
//			System.out.println(keyValue+"+加密+"+envalue);
			return envalue;
		}else{
			return keyValue;
		}
	}
	/**
	 * @author 韩飞
	 * @time Jul 14, 2011	
	 * @param keyValue
	 * @return 
	 * 方法功能: 通过固定密钥 解密信息
	 */
	public static String doDesString(String keyValue) {
		if(!"".equals(keyValue)){
			DesEncrypt des = DesEncrypt.getInstance();
			des.setKey(Application.KEY );
			String envalue = des.getDesString(keyValue);
//			System.out.println(keyValue+"+解密+"+envalue);
			return envalue;
		}else{
			return keyValue;
		}
		
	}
	
	/**
	 * @author 韩飞
	 * @time Jul 14, 2011	
	 * @param keyValue
	 * @return 
	 * 方法功能: 查询下拉选的内容信息 
	 */
	public List doSelect_Info(String typechar) {
		
		return SystemDao.SelSelect_Info(typechar);
		
	}
	/**
	 * @author 韩飞
	 * @time Jul 14, 2011	
	 * @param keyValue
	 * @return 
	 * 方法功能: 查询对应信息
	 */
	public Object doSel_Info(String asql,String where ) {
		StringBuilder sbSQL = new StringBuilder();
		sbSQL.append(AjaxSQL.doAjaxSQL(asql)+ where);
		CommonDBDao dbo= new CommonDBDao();
		return dbo.DBjsonMap(sbSQL.toString());
		
	}
	/**
	 * @author 韩飞
	 * @time Nov 22, 2011	
	 * @param purchaseid
	 * @return 
	 * 方法功能:  标记采购物料信息 到货 更新库存
	 */
	public String Stock_Label(String purchaseid) {
		CommonDBDao dbo= new CommonDBDao();
		if(dbo.executeSQL("update pres_purchase set progress='到货',deliverydate = '"+DateUtil.getTime()+"' where purchaseid=?", new SqlParameter(purchaseid))){
			flag = new StockDao().Stock_Label(dbo.queryListByPS(StockSql.UPD_MAKINGS_INFO, new SqlParameter(purchaseid),null));
		}
		if(flag){
			return DateUtil.getTime();
		}else{
			return "";
		}
		
	}
	
	/**
	 * @author 韩飞
	 * @time Nov 22, 2011	
	 * @param check 标记
	 * @param taskid 任务id
	 * @return 
	 * 方法功能:  标记排产打印 更新库存
	 */
	public boolean task_Check(String check,String taskid) {
		CommonDBDao dbo= new CommonDBDao();
		flag = dbo.insorupd("update machinetask set checkflag='"+check+"' where taskid='"+taskid+"'");
		return flag;
	}
	
	/**
	 * @author 韩飞
	 * @time Nov 22, 2011	
	 * @param orderid 订单id
	 * @param sign  标记
	 * @return 
	 * 方法功能:  标记发货状态 更新订单表
	 */
	public boolean sign_Order(String orderid,String sign,HttpServletRequest request) {
		CommonDBDao dbo= new CommonDBDao();
		String username = ((Hzsyspersonnel) request.getSession().getAttribute("admin_user")).getPersonname();
		flag = dbo.insorupd("update orderinfo set deliverflag='"+sign+"',delivertime='"+DateUtil.getDateTime()+"',deliveruser='"+username+"' where orderid = '"+orderid+"'");
		return flag;
	}
}	
